package com.weitu.train.certificate.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.weitu.permission.user.entity.User;
import com.weitu.train.certificate.entity.Certificate;
import com.weitu.train.certificate.entity.PersonFile;
import com.weitu.train.certificate.mapper.CertificateMapper;
import com.weitu.train.certificate.service.CertificateService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.weitu.util.DateTimeUtil;
import com.weitu.util.IDGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CertificateServiceImpl implements CertificateService {
    @Autowired
    private CertificateMapper certificateMapper;


    public Certificate selectById(String id) {
        return this.certificateMapper.selectById(id);
    }


    public int deleteById(String id) {
        return this.certificateMapper.deleteById(id);
    }

    public int updateByIdSelective(Certificate record) {
        return this.certificateMapper.updateByIdSelective(record);
    }

    @Override
    public int updateById(Map<String, Object> record) {
        return this.certificateMapper.updateById(record);
    }

    @Override
    public int insert(Map<String, Object> record) {
        return this.certificateMapper.insert(record);
    }


    public int insertSelective(Certificate record) {
        return this.certificateMapper.insertSelective(record);
    }

    @Override
    @Transactional
    public boolean insertCertificate(Map<String, Object> map) {
        boolean bCer=false;
        boolean bRec=false;
        int intc=this.certificateMapper.insert(map);
        if (intc>0){
            bCer=true;
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        String uuid = IDGenerator.getId();
        paramMap.put("id",uuid);
        paramMap.put("delState",0);
        paramMap.put("addUserId",map.get("addUserId"));
        paramMap.put("addDeptId",map.get("addDeptId"));
        paramMap.put("addTime",map.get("addTime"));
        paramMap.put("recordType",2);
        paramMap.put("certificateId",map.get("id"));
        paramMap.put("certificateCode",map.get("num"));
        paramMap.put("time",map.get("certificateTime"));
        if(map.get("nextReviewTime")!=null){
            paramMap.put("nextTime",map.get("nextReviewTime"));
        }
        if(map.get("nextChangedTime")!=null){
            paramMap.put("nextTime",map.get("nextChangedTime"));
        }
        if(map.get("nextReviewTime")!=null&&map.get("nextChangedTime")!=null){
            String nextReviewTime=map.get("nextReviewTime").toString();
            String nextChangedTime=map.get("nextChangedTime").toString();
            int nr= nextReviewTime.compareTo(nextChangedTime);
            if (nr==-1){
                paramMap.put("nextTime",nextReviewTime);
            }else {
                paramMap.put("nextTime",nextChangedTime);
            }
        }
        int intR=this.certificateMapper.insertRecords(paramMap);
        if (intR>0){
            bRec=true;
        }
        return bCer&&bRec;
    }

    @Override
    @Transactional
    public boolean updateCertificate(Map<String, Object> map) {
        boolean bcer=false;
        int ibcer=this.certificateMapper.updateById(map);
        if (ibcer>0){
            bcer=true;
        }
        String dateTime = DateTimeUtil.formatterDateTime();
        map.put("addTime", dateTime);
        boolean bcert=addRecords(map);
        return bcer&&bcert;
    }

    @Override
    public List<Certificate> selectAllCertificate(Map<String, Object> map) {
        return this.certificateMapper.selectAllCertificate(map);
    }

    @Override
    public int selectAllCertificateCount(Map<String, Object> map) {
        return this.certificateMapper.selectAllCertificateCount(map);
    }

    @Override
    public List<Map<String, Object>> selectRecords(String certificateId) {
        return this.certificateMapper.selectRecords(certificateId);
    }

    @Override
    public List<PersonFile> selectFiles(Map<String, Object> map) {
        return this.certificateMapper.selectFiles(map);
    }

    @Override
    public int selectFilesCount(Map<String, Object> map) {
        return this.certificateMapper.selectFilesCount(map);
    }

    /**
     * 员工培训档案员工记录
     * @param userId
     * @return
     */
    @Override
    public List<Map<String, Object>> selectRecordsByUserId(String userId) {
        return certificateMapper.selectRecordsByUserId(userId);
    }

    @Override
    public List<Map<String, Object>> selectCertByUserId(String userId) {
        return certificateMapper.selectCertByUserId(userId);
    }

    private boolean addRecords(Map<String,Object> paramMap){
        boolean boo=false;
        String certificateId=paramMap.get("id").toString();
        String dateTime=paramMap.get("addTime").toString();
        String loginUserId=paramMap.get("addUserId").toString();
        String loginUserDeptId=paramMap.get("addDeptId").toString();
        String num=paramMap.get("num").toString();
        if (paramMap.get("cerTb")!=null){
            String dataStr = paramMap.get("cerTb").toString();
            List<Map> list=addEditTableLists(dataStr,certificateId,dateTime,loginUserId,loginUserDeptId,num);
            paramMap.put("cerTbList",list);
            int dint=this.certificateMapper.deleteByCertificateId(certificateId);
            int aint=this.certificateMapper.insertRecordsList(paramMap);
            if (aint>0){
                boo=true;
            }
        }else {
            int dint=this.certificateMapper.deleteByCertificateId(certificateId);
            boo=true;
        }
        return boo;
    }

    private List<Map> addEditTableLists(String dataStr,String certificateId,String dateTime,String loginUserId,String loginUserDeptId,String num){
        List<Map> list = JSONArray.parseArray(dataStr,Map.class);
        for(Map m:list){
            String uuid = IDGenerator.getId();
            m.put("id",uuid);
            m.put("delState",0);
            m.put("certificateId",certificateId);
            m.put("addTime", dateTime);
            m.put("addUserId", loginUserId);
            m.put("addDeptId", loginUserDeptId);
            m.put("certificateCode", num);
        }
        return list;
    }
}